package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Task;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class TaskDao {
	private DBHelp<Task> db=new DBHelp<Task>();
	
	
	public List<Task> findByUserId(String id) {
		String sql="SELECT id,`name`,starttime,endtime,description,isImport,progress,t_user_id,t_goal_id,stat,t_project_id FROM t_task WHERE t_user_id=?";
		return db.executeQueryForList(sql, new TaskRowMapper(), id);
	}
	
	public List<Task> findByGoalId(String goalid) {
		String sql="SELECT id,`name`,starttime,endtime,description,isImport,progress,t_user_id,t_goal_id,stat,t_project_id FROM t_task WHERE t_goal_id=?";
		return db.executeQueryForList(sql, new TaskRowMapper(), goalid);
	}
	public boolean save(Task t) {
		String sql="INSERT INTO t_task(id,`name`,starttime,endtime,description,isImport,progress,t_user_id,t_goal_id,stat,t_project_id) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		return db.executeSQL(sql, t.getId(),t.getName(),t.getStarttime(),t.getEndtime(),t.getDescription(),t.getIsImport(),t.getProgress(),t.getT_user_id(),t.getT_goal_id(),t.getStat(),t.getT_project_id());
	}
	public Task findByTaskId(String taskid) {
		String sql="SELECT id,`name`,starttime,endtime,description,isImport,progress,t_user_id,t_goal_id,stat,t_project_id FROM t_task WHERE id=?";
		return db.executeQueryForObject(sql, new TaskRowMapper(),taskid);
	}
	public boolean update(String name,
			String starttime, String endtime, String desc, String stat,
			String isImport, String progress,String taskid) {
		String sql="UPDATE t_task SET `name`=?,starttime=?,endtime=?,description=?,isImport=?,progress=?,stat=? WHERE id=?";
		return db.executeSQL(sql, name,starttime,endtime,desc,isImport,progress,stat,taskid);
	}

	
	
	private class TaskRowMapper implements RowMapper<Task>{

		public Task mapRow(ResultSet rs) throws SQLException {
			Task t=new Task();
			t.setId(rs.getString("id"));
			t.setName(rs.getString("name"));
			t.setStarttime(rs.getString("starttime"));
			t.setEndtime(rs.getString("endtime"));
			t.setDescription(rs.getString("description"));
			t.setIsImport(rs.getString("isImport"));
			t.setStat(rs.getString("stat"));
			t.setProgress(rs.getString("progress"));
			t.setT_user_id(rs.getString("t_user_id"));
			t.setT_goal_id(rs.getString("t_goal_id"));
			t.setT_project_id(rs.getString("t_project_id"));
			return t;
		}
		
	}



	public List<Task> findByUserIdAndProjectId(String id,String projectid) {
		String sql="SELECT id,`name`,starttime,endtime,description,isImport,progress,t_user_id,t_goal_id,stat,t_project_id FROM t_task WHERE t_user_id=? and t_project_id=?";
		return db.executeQueryForList(sql, new TaskRowMapper(), id,projectid);
	}
	public List<Task> findByProjectId(String projectid) {
		String sql="SELECT id,`name`,starttime,endtime,description,isImport,progress,t_user_id,t_goal_id,stat,t_project_id FROM t_task WHERE t_project_id=?";
		return db.executeQueryForList(sql, new TaskRowMapper(), projectid);
	}

	public boolean delById(String taskid) {
		String sql="delete from t_task where id=?";
		return db.executeSQL(sql, taskid);
	}

	public boolean delByGoalId(String goalid) {
		String sql="delete from t_task where t_goal_id=?";
		return db.executeSQL(sql,goalid);
		
	}
	

}
